﻿using SD.Common;
using SD.Infrastructure.DTOBase;
using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SD.Infrastructure.WPF.Extensions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.ServiceModel.Extensions;
using System.Threading.Tasks;
using System.Windows;
using SlamDunk.MES.IAppService.DTOs.Outputs.ExecutionContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.ResourceContext;
using SlamDunk.MES.IAppService.Interfaces;
using SlamDunk.MES.Presentation.Enums;
using SlamDunk.MES.Presentation.Maps;
using SlamDunk.MES.Presentation.Models;
using SlamDunk.MES.Presentation.Models.NotifyModels;

namespace SlamDunk.MES.Client.ViewModels.Schedule
{
    /// <summary>
    /// 工单分派视图模型
    /// </summary>
    public class DispatchViewModel : ScreenBase
    {
        #region # 字段及构造器
        /// <summary>
        /// 作业管理服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IExecutionContract> _iExecutionContract;

        /// <summary>
        /// 资源定义服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IResourceContract> _iResourceContract;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public DispatchViewModel(ServiceProxy<IExecutionContract> iExecutionContract, ServiceProxy<IResourceContract> iResourceContract)
        {
            this._iExecutionContract = iExecutionContract;
            this._iResourceContract = iResourceContract;
        }
        #endregion

        #region 属性
        #region 分派类型列表
        /// <summary>
        /// 分派类型列表
        /// </summary>
        [DependencyProperty]
        public IDictionary<string, string> DispatchTypeList { get; set; }
        #endregion

        #region 分派信息列表
        /// <summary>
        /// 分派信息列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<IdentiferNameNm> DispatchList { get; set; }
        #endregion

        #region 已选分派类型
        /// <summary>
        /// 已选分派类型
        /// </summary>
        [DependencyProperty]
        public DispatchTypeEnum? SelectedDispatchType { get; set; }
        #endregion

        #region 已选分派信息
        /// <summary>
        /// 已选分派信息
        /// </summary>
        [DependencyProperty]
        public IdentiferNameNm SelectedDispatch { get; set; }
        #endregion

        #region 工单Id
        /// <summary>
        /// 工单Id
        /// </summary>
        [DependencyProperty]
        public Guid? WorkOrderId { get; set; }
        #endregion

        #region 工厂模型Id
        /// <summary>
        /// 工厂模型Id
        /// </summary>
        [DependencyProperty]
        public Guid? FacilityId { get; set; }
        #endregion
        #endregion

        #region 方法

        #region 加载 —— async Task Load(WorkOrderInfo workOrderInfo)
        /// <summary>
        /// 加载
        /// </summary>
        public async Task Load(WorkOrderInfo workOrderInfo)
        {
            WorkOrderId = workOrderInfo.Id;
            FacilityId = workOrderInfo.FacilityId;
            this.DispatchTypeList = typeof(DispatchTypeEnum).GetEnumMembers();
        }
        #endregion

        #region 分配类型事件
        /// <summary>
        /// 分配类型事件
        /// </summary>
        public async void LoadDispatchType()
        {
            if (SelectedDispatchType == DispatchTypeEnum.Person)
            {
                PageModel<PersonInfo> Persons = await Task.Run(() => this._iResourceContract.Channel.GetPersonsByPage(
               null,//关键字
               FacilityId,//工厂模型Id
               null, //人员类Id
               1,
               int.MaxValue));
                IEnumerable<IdentiferNameNm> Personsmodels = Persons.Datas.Select(x => x.ToIdentiferName());
                DispatchList = new ObservableCollection<IdentiferNameNm>(Personsmodels);
            }
            else
            {
                PageModel<TeamInfo> Teams = await Task.Run(() => this._iResourceContract.Channel.GetTeamsByPage(
              null,//关键字
              FacilityId,//工厂模型Id
              1,
              int.MaxValue));
                IEnumerable<IdentiferNameNm> Teamsmodels = Teams.Datas.Select(x => x.ToIdentiferName());
                DispatchList = new ObservableCollection<IdentiferNameNm>(Teamsmodels);
            }
        }
        #endregion

        #region 提交 —— async void Submit()
        /// <summary>
        /// 提交
        /// </summary>
        public async void Submit()
        {
            #region # 验证
            if (SelectedDispatchType == null)
            {
                MessageBox.Show("请选择分派类型！", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            if (SelectedDispatch == null)
            {
                MessageBox.Show("请选择分派信息！", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            #endregion


            this.Busy();
            if (SelectedDispatchType == DispatchTypeEnum.Person)
            {
                await Task.Run(() => this._iExecutionContract.Channel.DispatchWorkOrderToPerson(
             WorkOrderId.Value, SelectedDispatch.Id));
            }
            else
            {
                await Task.Run(() => this._iExecutionContract.Channel.DispatchWorkOrderToTeam(
            WorkOrderId.Value, SelectedDispatch.Id));
            }

            await base.TryCloseAsync(true);
            this.Idle();
        }
        #endregion
        #endregion
    }
}
